// 笔试强训day24:判断是不是平衡二叉树

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param pRoot TreeNode类 
     * @return bool布尔型
     */
    int IsBalancedTree(TreeNode* pRoot)
    {
        if(pRoot == nullptr)
            return 0;

        int leftHeight = IsBalancedTree(pRoot->left);
        int rightHeight = IsBalancedTree(pRoot->right);
        if(leftHeight - rightHeight > 1 || rightHeight - leftHeight > 1 || rightHeight == -1 || leftHeight == -1)
            return -1;
        else
            return max(leftHeight, rightHeight) + 1;
    }

    bool IsBalanced_Solution(TreeNode* pRoot) {
        // write code here
        return (IsBalancedTree(pRoot) != -1);
    }
};